package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    //条件分页查询
    @GetMapping("/page")
    public Result<PageResult> page(CategoryPageQueryDTO pageQueryDTO){
        log.info("分页查询：{}", pageQueryDTO);
        PageResult pageResult = categoryService.page(pageQueryDTO);
        return Result.success(pageResult);
    }

    //修改分类
    @PutMapping
    public Result updateById(@RequestBody CategoryDTO categoryDTO){
        categoryService.update(categoryDTO);
        return Result.success();
    }

    //根据id查询分类
    @GetMapping("/{id}")
    public Result<Category> getById(@PathVariable Long id){
        Category category = categoryService.getById(id);
        return Result.success(category);
    }

    //启用或禁用分类的状态
    @PutMapping("/status/{status}/{id}")
    public Result startOrStop(@PathVariable Integer status, @PathVariable Long id){
        categoryService.updateStatus(status,id);
        return Result.success();
    }

    //新增分类
    @PostMapping
    public Result add(@RequestBody CategoryDTO categoryDTO){
        log.info("新增分类：{}", categoryDTO);
        categoryService.add(categoryDTO);
        return Result.success();
    }

    //根据id删除分类
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Long id){
        categoryService.delete(id);
        return Result.success();
    }

    //根据类型查询分类信息
    @GetMapping("/list")
    public Result<List<Category>> list(Integer type){
        List<Category> list = categoryService.list(type);
        return Result.success(list);
    }
}
